﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmThaydoixeploai : Form
    {
        bool temp;       //tùy chọn insert or update
        int count;      //Số lượng sinh viên
        int index;      //ví trí của sinh viên đó trong dữ liệu
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");
        string sql = "select * from T17_Bang_xep_loai";
        public frmThaydoixeploai()
        {
            InitializeComponent();
            disable_textbox();
        }
        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            return ds;
        }

        public void get_gridviewdanhsachxl()
        {
            DataSet ds = FillDataset("select * from T17_Bang_xep_loai");
            gCol_Xeploai.FieldName = "XLoai";
            gCol_Diemtu.FieldName = "Diemtu";
            gCol_Diemdencan.FieldName = "Diemdencan";
            gridControl1.DataSource = ds.Tables[0];
        }
        public void get_Tabdanhsachxl(string sql, DataRow row)
        {
            cbXeploai.Text = row["XLoai"].ToString();
            cbDiemtu.Text = row["Diemtu"].ToString();
            cbDiemdencan.Text = row["Diemdencan"].ToString();
            label1.Text = row["ID"].ToString();
        }

        private void frmThaydoixeploai_Load(object sender, EventArgs e)
        {

            get_gridviewdanhsachxl();
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[0];
            count = ds.Tables[0].Rows.Count;
            get_Tabdanhsachxl(sql, row);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string masv = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gCol_Xeploai).ToString();
            string str = "select * from T17_Bang_xep_loai where XLoai = N'" + masv + "'";
            DataSet ds = FillDataset(str);
            DataRow row = ds.Tables[0].Rows[0];
            int[] i = gridView1.GetSelectedRows();
            index = i[0];
            get_Tabdanhsachxl(str, row);
        }


        private void btnVedau_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[0];
            get_Tabdanhsachxl(sql, row);
            index = ds.Tables[0].Rows.IndexOf(row);

        }

        private void btnLui_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            index--;
            if (index >= 0)
            {
                DataSet ds = FillDataset(sql);
                DataRow row = ds.Tables[0].Rows[index];
                get_Tabdanhsachxl(sql, row);
                index = ds.Tables[0].Rows.IndexOf(row);
            }
            else
            {
                index++;
                MessageBox.Show("Bạn đang ở mẫu tin đầu tiên");
            }
        }

        private void bntToi_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            index++;
            if (index < count)
            {
                DataSet ds = FillDataset(sql);
                DataRow row = ds.Tables[0].Rows[index];
                get_Tabdanhsachxl(sql, row);
                index = ds.Tables[0].Rows.IndexOf(row);
            }
            else
            {
                index--;
                MessageBox.Show("Bạn đang ở mẫu tin cuối cùng");
            }
        }

        private void btnVecuoi_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[count - 1];
            get_Tabdanhsachxl(sql, row);
            index = ds.Tables[0].Rows.IndexOf(row);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            btnVedau.Enabled = false;
            btnVecuoi.Enabled = false;
            btnLui.Enabled = false;
            btnToi.Enabled = false;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnThoat.Enabled = false;
            btnLuu.Enabled = true;
            btnBoqua.Enabled = true;
            temp = false;
            cbXeploai.Text = "";
            cbDiemtu.Text = "";
            cbDiemdencan.Text = "";
            enable_textbox();
        }

        public void query(string command)
        {

            connection.Open();
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = command;
            SqlDataReader dr = sqlCommand.ExecuteReader();
            connection.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa không?.", "Quản lý sinh viên", MessageBoxButtons.OKCancel, MessageBoxIcon.None) == DialogResult.OK)
            {
                try
                {
                    string str = "delete from T17_Bang_xep_loai where ID=N'" + label1.Text + "' ";
                    query(str);
                    get_gridviewdanhsachxl();
                    btnLui_Click(sender, e);
                }
                catch (Exception err)
                {
                    MessageBox.Show("Error: " + err.Message);
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            temp = true;
            btnVedau.Enabled = false;
            btnVecuoi.Enabled = false;
            btnLui.Enabled = false;
            btnToi.Enabled = false;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnThoat.Enabled = false;
            btnLuu.Enabled = true;
            btnBoqua.Enabled = true;
            enable_textbox();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                if (temp == false)
                {
                    string str = "insert into T17_Bang_xep_loai(Xeploai, Diemtu, Diemdencan) values (N'" + cbXeploai.Text + "', N'" + cbDiemtu.Text + "', N'" + cbDiemdencan.Text + "')";
                    query(str);
                }
                else
                {
                    string str = "update T17_Bang_xep_loai set XLoai = N'" + cbXeploai.Text + "', Diemtu =  N'" + cbDiemtu.Text + "',Diemdencan = N'" + cbDiemdencan.Text + "' where ID = N'" + label1.Text + "'";
                    query(str);
                }
                disable_textbox();
                btnVedau.Enabled = true;
                btnVecuoi.Enabled = true;
                btnLui.Enabled = true;
                btnToi.Enabled = true;
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnThoat.Enabled = true;
                btnLuu.Enabled = false;
                btnBoqua.Enabled = false;
                get_gridviewdanhsachxl();
            }
            else
                MessageBox.Show("Bạn chưa nhập dữ liệu cần thiết", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }

        private void btnBoqua_Click(object sender, EventArgs e)
        {
            btnVedau.Enabled = true;
            btnVecuoi.Enabled = true;
            btnLui.Enabled = true;
            btnToi.Enabled = true;
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnThoat.Enabled = true;
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[index];
            get_Tabdanhsachxl(sql, row);
            disable_textbox();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void disable_textbox()
        {
            cbXeploai.Enabled = false;
            cbDiemtu.Enabled = false;
            cbDiemdencan.Enabled = false;
        }

        public void enable_textbox()
        {
            cbXeploai.Enabled = true;
            cbDiemtu.Enabled = true;
            cbDiemdencan.Enabled = true;
        }

        
    }
}
